package cn.csc.module.order.service.logistics;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.order.controller.admin.logistics.vo.OrderLogisticsCreateReqVO;
import cn.csc.module.order.controller.admin.logistics.vo.OrderLogisticsExportReqVO;
import cn.csc.module.order.controller.admin.logistics.vo.OrderLogisticsPageReqVO;
import cn.csc.module.order.controller.admin.logistics.vo.OrderLogisticsUpdateReqVO;
import cn.csc.module.order.convert.logistics.OrderLogisticsConvert;
import cn.csc.module.order.dal.dataobject.logistics.OrderLogisticsDO;
import cn.csc.module.order.dal.mysql.logistics.OrderLogisticsMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.order.enums.ErrorCodeConstants.ORDER_LOGISTICS_NOT_EXISTS;

/**
 * 订单物流 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class OrderLogisticsServiceImpl implements OrderLogisticsService {

    @Resource
    private OrderLogisticsMapper logisticsMapper;

    @Override
    public BigInteger createLogistics(OrderLogisticsCreateReqVO createReqVO) {
        // 插入
        OrderLogisticsDO logistics = OrderLogisticsConvert.INSTANCE.convert(createReqVO);
        logisticsMapper.insert(logistics);
        // 返回
        return logistics.getId();
    }

    @Override
    public void updateLogistics(OrderLogisticsUpdateReqVO updateReqVO) {
        // 校验存在
        validateLogisticsExists(updateReqVO.getId());
        // 更新
        OrderLogisticsDO updateObj = OrderLogisticsConvert.INSTANCE.convert(updateReqVO);
        logisticsMapper.updateById(updateObj);
    }

    @Override
    public void deleteLogistics(BigInteger id) {
        // 校验存在
        validateLogisticsExists(id);
        // 删除
        logisticsMapper.deleteById(id);
    }

    private void validateLogisticsExists(BigInteger id) {
        if (logisticsMapper.selectById(id) == null) {
            throw exception(ORDER_LOGISTICS_NOT_EXISTS);
        }
    }

    @Override
    public OrderLogisticsDO getLogistics(BigInteger id) {
        return logisticsMapper.selectById(id);
    }

    @Override
    public List<OrderLogisticsDO> getLogisticsList(Collection<BigInteger> ids) {
        return logisticsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderLogisticsDO> getLogisticsPage(OrderLogisticsPageReqVO pageReqVO) {
        return logisticsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrderLogisticsDO> getLogisticsList(OrderLogisticsExportReqVO exportReqVO) {
        return logisticsMapper.selectList(exportReqVO);
    }

}
